# -*- coding: utf-8 -*-
'''
Created on Jun 14, 2013

@author: LONG HOANG GIANG
'''
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from urlparse import urljoin
import pyLib
import re
import workerpool
import json
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')


URL = 'http://nanakwon.wordpress.com/ng%C6%B0%E1%BB%9Di-tinh-ba-d%E1%BA%A1o-3/'

OUTPUT = "/longhoanggiang/truyentext/nguoitinhbadao"
OUTPUT_FILES = OUTPUT + "/files"
pyLib.createIfNotExistsPath(OUTPUT_FILES)


def getDetail(item):
    url = item['url']
    logging.info("get detail of {0}".format(item['name']))
    tree = pyLib.loadWeb(url).build_tree()
    contentNode = tree.xpath("//div[@class='entry-content']")[0]
    pyLib.Etree.cleanNodeNextSibling(contentNode.xpath(".//div[@class='wpa']"), True)
    contennt = pyLib.cleanHTML(pyLib.Etree.tostring(contentNode)).strip()
    contennt = re.sub(r"\n", "<br />", contennt)
    contennt = '''<b>{0}</b><br /><hr /><br />{1}'''.format(item['name'], contennt)
    print contennt
    pyLib.gz_file_put_content(OUTPUT_FILES + "/" + item['path'], json.dumps(contennt))
    
#    print pyLib.Etree.tostring(contentNode)


def getListChapter():
    
    tree = pyLib.loadWeb(URL).build_tree()
    data = []
    for item in tree.xpath("//div[@class='entry-content']//p[@style='text-align:center;']//a"):
        name = pyLib.stringify(item).strip('-').strip('– ').strip().strip('      ').strip('  -').strip()
        if name == '' or name == 'C' or name == 'Chươn': continue
        href = item.get('href')
        if name == 'hương 21': name = "Chương 21"
        if name == 'g 48': name = 'Chương 48'
        print name, href
        data.append({'name': pyLib.toUpper(name), 'url': href, 'path': pyLib.crc32unsigned(href)})
        
    return data
    
    
def process():
    
    data = getListChapter()
    
    pool = workerpool.WorkerPool(size=5)
    pool.map(getDetail, data)
    pool.shutdown()
    pool.wait()
    
    for d in data:
        del d['url']
    pyLib.gz_file_put_content(OUTPUT + "/data", pyLib.encryptCipher(json.dumps(data)))
    
    
if __name__ == '__main__':
    
#    getListChapter()
#    getDetail({'name': 'Aaa', 'url': 'http://nanakwon.wordpress.com/2011/05/05/ntbd-ch%C6%B0%C6%A1ng-41/', 'path': 'a'})
    process()
    
    logging.info("finished")
    os._exit(1)
    
    